import VueRouter from 'vue-router';
import Vue from 'vue';
import { Toast } from 'vant';
Vue.use(VueRouter);
const routes = [
    {
        path: '/',
        redirect: '/index'
    },
    {
        name: 'index',
        path: '/index',
        component: () => import(/* webpackChunkName: "chunk01" */'../pages/IndexView'),
        meta: {
            title: '商城首页',
            requireLogin: false
        }
    },
    {
        name: 'goodsList',
        path: '/goodsList',
        component: () => import(/* webpackChunkName: "chunk01" */'../pages/GoodsList'),
        meta: {
            title: '商品列表',
            requireLogin: false
        }
    },
    {
        name: 'goods',
        path: '/goods',
        component: () => import(/* webpackChunkName: "chunk01" */'../pages/GoodsView'),
        meta: {
            title: '商品详情',
            requireLogin: false

        }
    },
    {
        name: 'cart',
        path: '/cart',
        component: () => import(/* webpackChunkName: "chunk02" */'../pages/CartView'),
        meta: {
            title: '购物车',
            requireLogin: true

        }
    },
    {
        name: 'search',
        path: '/search',
        component: () => import(/* webpackChunkName: "chunk02" */'../pages/SearchView'),
        meta: {
            title: '搜索',
            requireLogin: false
        }
    },
    {
        name: 'user',
        path: '/user',
        component: () => import(/* webpackChunkName: "chunk02" */'../pages/UserView'),
        meta: {
            title: '会员中心',
            requireLogin: true

        }
    },
    {
        name: 'addressList',
        path: '/addressList',
        component: () => import(/* webpackChunkName: "chunk02" */'../pages/AddressList'),
        meta: {
            title: '地址管理',
            requireLogin: true

        }
    },
    {
        name: 'addressAdd',
        path: '/addressAdd',
        component: () => import(/* webpackChunkName: "chunk02" */'../pages/AddressAdd'),
        meta: {
            title: '地址新增',
            requireLogin: true

        }
    },
    {
        name: 'cate',
        path: '/cate',
        component: () => import(/* webpackChunkName: "chunk03" */'../pages/CateView'),
        meta: {
            title: '商品分类',
            requireLogin: false

        }
    },
    {
        name: 'reg',
        path: '/reg',
        component: () => import(/* webpackChunkName: "chunk04" */'../pages/RegView'),
        meta: {
            title: '会员注册',
            requireLogin: false
        }
    },
    {
        name: 'login',
        path: '/login',
        component: () => import(/* webpackChunkName: "chunk04" */'../pages/LoginView'),
        meta: {
            title: '会员登录',
            requireLogin: false
        }
    }
];
const originPush = VueRouter.prototype.push;
// 捕获路由导航守卫中重定向路由报错
VueRouter.prototype.push = function (location) {
    return originPush.call(this, location).catch((err) => err);
}
const router = new VueRouter({
    routes
});

router.beforeEach((to, from, next) => {
    if (to.meta.requireLogin) {
        // 检测登录状态
        const { token } = JSON.parse(localStorage.getItem('userLoginInfo') || '{}');
        if (!token) {
            Toast('您还没有登录');
            return next({ name: "login" });
        } else {
            next();
        }
    } else {
        next();
    }

    if (to.meta.title) {
        document.title = to.meta.title;
    }
});
export default router;